package com.hua.common.photopicker.view;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.hua.common.BaseActivity;
import com.hua.common.R;
import com.hua.common.photopicker.util.MediaStoreHelper;
import com.hua.common.photopicker.util.PhotoDirectory;
import java.util.List;


public class PhotosListActivity extends BaseActivity implements OnItemClickListener {

	private ListView listView=null;
	private PhotoListAdapter adapter=null;

	private boolean isForResoult=false;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.initViews();
		this.setPageTitle("列表");
		this.initData();
	}

	/**
	 * 初始化界面
	 */
	private void initViews() {
		this.setContentViewItem(R.layout.photolist);
		this.listView=(ListView) this.findViewById(R.id.phlist);
	    this.listView.setOnItemClickListener(this);

		this.isForResoult=getIntent().getBooleanExtra("isforresoult",false);
	}
	
	private void initData(){
		MediaStoreHelper.getPhotoDirs(this, new MediaStoreHelper.PhotosResultCallback() {
			@Override
			public void onResultCallback(List<PhotoDirectory> directories) {
				adapter = new PhotoListAdapter(PhotosListActivity.this, directories);
				listView.setAdapter(adapter);
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		PhotoDirectory photoDirectory = adapter.getItem(position);
		Bundle bundle = new Bundle();
		bundle.putSerializable("data", photoDirectory);
		bundle.putString("name", photoDirectory.getName());
		bundle.putBoolean("isforresoult",isForResoult);
		Intent intent = new Intent();
		intent.putExtras(bundle);
		intent.setClass(this, PhotoGridActivity.class);
		if(this.isForResoult){
			startActivityForResult(intent,0);
		}else {
			startActivity(intent);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==0){
			setResult(0,data);
			finish();
		}
	}
}
